import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Setting extends StatefulWidget {
  const Setting({super.key});

  @override
  State<Setting> createState() => _SettingState();
}

class _SettingState extends State<Setting> {

  final _controller = TextEditingController();
  String server_url = "";

  Future<void> _loadConfig() async{
    final prefs = await SharedPreferences.getInstance();
    var serverUrl = prefs.getString("server_url") ?? "http://127.0.0.1:8099";
    _controller.text = serverUrl;
    setState(() {
      server_url = serverUrl;
    });
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadConfig();

  }



  @override
  Widget build(BuildContext context) {

    return  Scaffold(
      appBar: AppBar(
        title: Text("系统配置"),
      ),
        body: Container(
          padding: EdgeInsets.only(left: 20,top:100,right: 20,bottom: 40),
          child: Column(
            children: [
              TextField(
                controller: _controller,
                onChanged: (val)=>{
                  setState(() {
                   server_url = val;
                  })
                },
                decoration: InputDecoration(
                  labelText: "服务器地址",
                  suffixIcon:server_url.isNotEmpty?
                    IconButton(
                    icon: Icon(Icons.clear),
                    onPressed: () {
                      _controller.clear(); // 清空输入内容
                      setState(() {
                        server_url = "";
                      }); // 更新 UI
                    },
                  ):null

                ),
              ),
              SizedBox(height: 40,),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.green[300],
                      foregroundColor: Colors.white
                    ),
                    onPressed: () async{
                      var text = _controller.text;


                      if(text.isEmpty){
                        Fluttertoast.showToast(
                            msg: "服务器地址不能为空",
                            toastLength: Toast.LENGTH_SHORT,
                            gravity: ToastGravity.TOP,
                            timeInSecForIosWeb: 2,
                            backgroundColor: Colors.red[300],
                            textColor: Colors.white,
                            fontSize: 14.0
                        );
                        return;
                      }
                      final prefs = await SharedPreferences.getInstance();
                      await prefs.setString('server_url', text);
                      Fluttertoast.showToast(
                          msg: "保存成功",
                          toastLength: Toast.LENGTH_SHORT,
                          gravity: ToastGravity.TOP,
                          timeInSecForIosWeb: 2,
                          backgroundColor: Colors.green[400],
                          textColor: Colors.white,
                          fontSize: 14.0
                      );
                    }, child: Text("保存")),
              )
            ],
          ),
        ),

    );
  }
}
